package com.xms.file.service;

import com.xms.core.config.SystemEntityNames;
import com.xms.core.data.Entity;
import com.xms.core.exception.XmsException;
import com.xms.sdk.service.IDataCreater;
import com.xms.utils.CollectionUtil;
import com.xms.utils.FileUtil;
import com.xms.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * AttachmentCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/9/14
 */
@Service
public class AttachmentCreaterService implements IAttachmentCreaterService {
    private IDataCreater _dataCreater;
    private IAttachmentSettings _attachmentSettings;

    @Autowired
    public void AttachmentCreaterService(IDataCreater dataCreater
            , IAttachmentSettings attachmentSettings) {
        _dataCreater = dataCreater;
        _attachmentSettings = attachmentSettings;
    }

    @Override
    public Entity create(UUID entityId, UUID objectId, Part file) throws XmsException, SQLException, IOException {
        String absoluteUploadFolder = _attachmentSettings.getAbsoluteUploadFolder();
        String uploadFolder = _attachmentSettings.getUploadFolder();
        UUID id = UUIDUtil.get();
        try {
            File uploadedFile = FileUtil.saveTo(file.getInputStream(), file.getSubmittedFileName(), absoluteUploadFolder);
            Entity ent = new Entity(SystemEntityNames.Attachment)
                    .setIdValue(id)
                    .setValue("name", file.getSubmittedFileName())
                    .setValue("filesize", file.getSize())
                    .setValue("mimetype", file.getContentType())
                    .setValue("cdnpath", uploadFolder + "/" + uploadedFile.getName())
                    .setValue("entityid", entityId)
                    .setValue("objectid", objectId);
            _dataCreater.create(ent);
            return ent;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Entity create(UUID entityId, UUID objectId, MultipartFile file) throws XmsException, SQLException, IOException {
        if (file.getSize() > 0) {
            String absoluteUploadFolder = _attachmentSettings.getAbsoluteUploadFolder();
            String uploadFolder = _attachmentSettings.getUploadFolder();
            UUID id = UUIDUtil.get();
            File uploadedFile = FileUtil.saveTo(file, absoluteUploadFolder);
            Entity ent = new Entity(SystemEntityNames.Attachment)
                    .setIdValue(id)
                    .setValue("name", file.getOriginalFilename())
                    .setValue("filesize", file.getSize())
                    .setValue("mimetype", file.getContentType())
                    .setValue("cdnpath", uploadFolder + "/" + uploadedFile.getName())
                    .setValue("entityid", entityId)
                    .setValue("objectid", objectId);
            _dataCreater.create(ent);
            return ent;
        }
        return null;
    }

    @Override
    public List<Entity> createMany(UUID entityId, UUID objectId, List<MultipartFile> files) throws XmsException, IOException {
        String absoluteUploadFolder = _attachmentSettings.getAbsoluteUploadFolder();
        String uploadFolder = _attachmentSettings.getUploadFolder();
        //附件
        List<Entity> attachments = new ArrayList<>();
        if (CollectionUtil.notEmpty(files)) {
            for (MultipartFile file : files) {
                if (file.getSize() > 0) {
                    UUID id = UUIDUtil.get();
                    File uploadedFile = FileUtil.saveTo(file, absoluteUploadFolder);
                    Entity ent = new Entity(SystemEntityNames.Attachment)
                            .setIdValue(id)
                            .setValue("name", file.getOriginalFilename())
                            .setValue("filesize", file.getSize())
                            .setValue("mimetype", file.getContentType())
                            .setValue("cdnpath", uploadFolder + "/" + uploadedFile.getName())
                            .setValue("entityid", entityId)
                            .setValue("objectid", objectId);
                    attachments.add(ent);
                }
            }
        }
        //保存附件
        if (attachments.size() > 0) {
            _dataCreater.createMany(attachments);
            return attachments;
        }
        return null;
    }
}
